// pages/welfareMall/goodDetail/index.js
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    pickerVisible: false,
    pickerValue: "",
    StatusBar: app.globalData.StatusBar,   //状态栏高度
    CustomBar: app.globalData.CustomBar,  //头部nav高度
    backIcon: true,   // 返回按钮是否显示
    title: "商品详情",  //页面标题
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    imgUrls: [],
    addCartVisible: false,
    goodsKindList: [],
    goodskindId: '',
    purchaseCount: 1,
    mallProductId:null,
    sendObj: [
      {
        type: "自提",
        active: ""
      },
      {
        type: "配送",
        active: ""
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('optionId',options.id)
    // 获取商品id
    this.setData({
      mallProductId: options.id
    })
    this.getCartMes()
    this.getDetail(options.id)
  },
  // 查看大图
  lookBigPic(e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  //请求商品详情数据
  getDetail(id) {
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'mallproduct/getMallProductDetail.do',
      method: 'post',
      data: {
        "userCode": wx.getStorageSync('userCode'),
        "mallproductId": id
      },
      success(res) {
        console.log('详情', res)
        const data = res.data
        if (data.goodsKindList > 0) {
          data.goodsKindList.forEach(item => {
            item.active = false
          })
        }
        // console.log('goodsKindList',data.goodsKindList)
        that.setData({
          goodsDesc: data.productInfo.goodsDesc,
          goodsName: data.productInfo.goodsName,
          goodsPrice: data.productInfo.goodsPrice,
          marketPrice: data.productInfo.marketPrice,
          repertory: data.productInfo.repertory,
          shopCartNum: data.productInfo.shopCartNum,
          ydPrice: data.productInfo.ydPrice,
          imgUrls: data.imgUrls,
          goodsKindList: data.goodsKindList
        })
        const length = data.imgUrls.length
        if (length > 0) {
          that.selectComponent("#swiper").dotObjInit()
        }
      }
    })
  },
  // 获取购物车中商品数
  getCartMes() {
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'shopcard/getShopCartNum.do',
      method: 'post',
      data: {
        userCode: wx.getStorageSync('userCode')
      },
      success(res) {
        console.log('购物车', res)
        that.setData({
          shopCartNum: res.data.shopCartNum
        })
      }
    })
  },
  updateCartNum() {
    var pages = getCurrentPages();//当前页面栈
    var mallIndex = pages[0]

    mallIndex.getCartMes()
    wx.navigateBack({
      delta: pages.length
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  handleChange({ detail }) {
    this.setData({
      purchaseCount: detail.value
    })
  },
  closeAddToCart() {
    this.setData({
      addCartVisible: false
    })
  },
  showAddToCart() {
    const that = this
    if (this.data.pickerValue) {
      this.setData({
        addCartVisible: true
      })
    } else {
      wx.showToast({
        title: '请先选择配送方式',
        image: '/assets/images/icon/error.png'
      })
    }
    this.setData({
      buyNow: false
    })
  },
  // 点击立即购买
  buyNow() {
    // this.showAddToCart()
    this.setData({
      addCartVisible: true
    })
    this.setData({
      buyNow: true
    })
  },
  // 立即购买
  addBuyNow() {
    const that = this
    var data = {
      "mallProductId": this.data.mallProductId,
      "userCode": wx.getStorageSync('userCode'),
      "goodskindId": this.data.goodskindId,
      "num": this.data.purchaseCount
    }
    console.log('参数', data)
    if (this.data.goodskindId.length === 0 || this.data.goodskindId === null || this.data.goodskindId === 'undefined'){
      wx.showToast({
        title: '没有选择分类',
        image: '/assets/images/icon/error.png'
      })
    } else {
    wx.request({
      url: app.globalData.baseUrl + 'shopcard/buyRightNow.do',
      method: 'post',
      data: data,
      success(res) {
        console.log('data', res)
        if (res.data.errorMsg) {
          wx.showToast({
            title: res.data.errorMsg,
            image: '/assets/images/icon/error.png'
          })
        } else {
          wx.navigateTo({
            url: '../cart/order/index?type=2&idArr=' + res.data.shopcartId
          })
        }
      }
    })
    }

  },
  // 加入购物车
  add() {
    // this.closeAddToCart();
    // wx.showToast({
    //   title: '已成功加入购物车',
    // })
    const that = this
    var data = {
      "mallProductId": this.data.mallProductId,
      "userCode": wx.getStorageSync('userCode'),
      "goodskindId": this.data.goodskindId,
      "num": this.data.purchaseCount,
      "sendMode": this.data.pickerValue
    }
    console.log('参数2', data)
    wx.request({
      url: app.globalData.baseUrl + 'shopcard/addShopcart.do',
      method: 'post',
      data: data,
      success(res) {
        console.log('加入返回',res)
        that.getCartMes()
        that.closeAddToCart();
        wx.showToast({
          title: '已成功加入购物车',
        })

      }
    })

  },
  // 颜色分类选择
  seled(e) {
    console.log(e.currentTarget)
    var length = this.data.goodsKindList.length;
    var index = e.currentTarget.dataset.index,
      id = e.currentTarget.dataset.id,
      activeItem = "goodsKindList[" + index + "].active",
      that = this;
    for (var i = 0; i < length; i++) {
      var item = "goodsKindList[" + i + "].active";
      that.setData({
        [item]: ""
      })
    }
    that.setData({
      [activeItem]: "active",
      goodskindId: id
    })
  },

  selSend(e) {
    var that = this,
      length = this.data.sendObj.length,
      index = e.target.dataset.index;
    let aciveItem = "sendObj[" + index + "].active";
    console.log(aciveItem)
    for (var i = 0; i < length; i++) {
      let item = "sendObj[" + i + "].active";
      that.setData({
        [item]: ''
      });
    }

    that.setData({
      [aciveItem]: "active",
      pickerValue: this.data.sendObj[index].type
    })

  },
  hidePicker() {
    this.setData({
      pickerVisible: false
    })
  },
  selPicker() {
    this.setData({
      pickerVisible: false
    })
  },
  showPicker() {
    this.setData({
      pickerVisible: true
    })
  },
  // 跳转页面函数
  toPage(e) {
    var url = e.currentTarget.dataset.url
    console.log(e.currentTarget.dataset.url)
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})